package com.net.framework.help.utils;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;

import com.net.framework.help.R;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Author：xiaobin.zhang
 * Email：154722566@qq.com
 * Date：2016/11/15 13:17
 * Desc：<--功能描述-->
 */
public class SelectPictureUtil {
    public static final int CAMERA_RESULT = 100;//拍照
    public static final int PHOTO_RESULT = 200;//相册
    private static File mPhotoFile;

    public static File getmPhotoFile() {
        return mPhotoFile;
    }

    /***
     * 选择了相册 ，跳入相册
     */
    public static void openPhoto(Activity activity) {
        Intent intent;
        if (Build.VERSION.SDK_INT < 19) {
            intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
        } else {
            intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        }
        activity.startActivityForResult(intent, PHOTO_RESULT);
    }


    public static void openCamera(Activity activity) {
        /* 选择了照相机拍摄,调起相机 */
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            String imageName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
            File f = new File(SaveImageUitle.path(activity));
            if (!f.exists()) {
                f.mkdirs();
            }
            String local_file = f.getAbsolutePath() + "/" + imageName + ".jpg";
            mPhotoFile = new File(local_file);
            try {
                mPhotoFile.createNewFile();
            } catch (IOException ex) {
                LogUtils.println("拍照创建异常" + ex.getMessage().toString());
                UIUtils.shortToast(R.string.create_fail_text);
                return;
            }

            Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            Uri photoUri = FileProvider.getUriForFile(
                    activity,
                    activity.getPackageName() + ".fileProvider",
                    mPhotoFile);
            takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
//            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPhotoFile));
            activity.startActivityForResult(takePhotoIntent, CAMERA_RESULT);
        } else {
            UIUtils.shortToast(R.string.sd_null_text);
        }
    }
}
